Use case: User application option-bytes configuration

Use case applicable in HAL2 for both FLASH and NVM.

Note

  • Option bytes may differ from one series to another. However, the sequence and programming model remain the same.

Important

In HAL1, option bytes are programmed through a single monolithic FLASH_OBProgramInitTypeDef structure passed to HAL_FLASHEx_OBProgram(), requiring the application to populate many fields even when configuring one feature.

HAL2 removes this composite struct in favor of feature-specific unitary APIs (for example, HAL_FLASH_ITF_OB_SetRDPLevel()) followed by an explicit HAL_FLASH_ITF_OB_Program() and HAL_FLASH_ITF_OB_Launch() sequence.

In HAL2, HAL_FLASH_ITF_OB_Unlock() unlocks OB control (instead of two calls in HAL1), and the launch step explicitly triggers the option-byte loader reset.

Sequence diagram

User application option-bytes configuration (HAL1)

@startuml
title: User application option-bytes configuration (HAL1)

hide footbox
participant "User Application" as User
participant "FLASH" as Flash
participant "System" as Sys

group #snow Initialization
   User -> Sys : HAL_Init()
   User -> Sys : SystemClock_Config()
   note right: FLASH latency updated according to bus frequency
end

group #snow FLASH Unlock
   User -> Flash : HAL_FLASH_Unlock()
end

group #snow FLASH Option Bytes Unlock
   User -> Flash : HAL_FLASH_OB_Unlock()
end

group #snow Prepare Option Bytes Configuration
   User -> User : Fill FLASH_OBProgramInitTypeDef structure
end

group #snow FLASH Option Bytes Programming
   User -> Flash : HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct)
end

group #snow FLASH Option Bytes Lock
   User -> Flash : HAL_FLASH_OB_Lock()
end

group #snow FLASH Lock (recommended)
   User -> Flash : HAL_FLASH_Lock()
end

@enduml

Code snippet

HAL1

HAL2

/* Example: Configure RDP level 1 and BOR level 2 (HAL1) */
FLASH_OBProgramInitTypeDef ob_cfg;
FLASH_OBProgramInitTypeDef ob_curr;

/* Unlock main FLASH and option bytes interface */
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();

/* (Optional) Retrieve current option bytes for comparison */
HAL_FLASHEx_OBGetConfig(&ob_curr);

/* Select the option bytes to modify */
ob_cfg.OptionType = OPTIONBYTE_RDP | OPTIONBYTE_BOR;

/* Configure the desired option bytes */
ob_cfg.RDPLevel = OB_RDP_LEVEL_1;
ob_cfg.BORLevel = OB_BOR_LEVEL_2;

/* Program the selected option bytes */
if (HAL_FLASHEx_OBProgram(&ob_cfg) != HAL_OK)
{
  /* handle error */
}

/* Generate option byte reload / reset */
HAL_FLASH_OB_Launch();

/* Lock main FLASH and option bytes */
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
/* Example: Configure RDP level 1 and BOR level 2 (HAL2) */

/* Unlock option bytes interface */
HAL_FLASH_ITF_OB_Unlock(HAL_FLASH);

if (HAL_FLASH_ITF_OB_SetRDPLevel(HAL_FLASH, HAL_FLASH_OB_RDP_LEVEL_1) != HAL_OK)
{
  /* handle error */
}

if (HAL_FLASH_ITF_OB_SetBORLevel(HAL_FLASH, HAL_FLASH_OB_BOR_LEVEL_2) != HAL_OK)
{
  /* handle error */
}

/* Program buffered option byte changes */
if (HAL_FLASH_ITF_OB_Program(HAL_FLASH) != HAL_OK)
{
  /* handle error */
}

/* Launch to reload the new option bytes (may trigger reset) */
HAL_FLASH_ITF_OB_Launch(HAL_FLASH);

/* Lock option bytes control */
HAL_FLASH_ITF_OB_Lock(HAL_FLASH);

Key changes

  1. Unitary, feature-specific setters replace the monolithic HAL1 option-byte configuration struct.

  2. Option-byte writeback and reload are explicit in HAL2 (OB_Program + OB_Launch).

  3. Option-byte control is confined to the ITF layer, clarifying separation of concerns.